﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MenusModify.aspx.cs" Inherits="SME.Food.Tricon.Web.AdminJS.MenusManage.MenusModify" %>

<script type="text/javascript">
    function Getupdate(obj) {
        var trobj= $(obj).parent().parent().parent().find("td");
        trobj.eq(3).text("");
        $("<input type='text' id='Num'  name='lookup[" + trobj.eq(0).text() + "].Num' value='1' size='12' class='required textInput'>").appendTo(trobj.eq(3).text(""));
        var idsUp = $("#hidUpd", $("#MenusModify")).val();
        idsUp += trobj.eq(0).text() + ",";
        $("#hidUpd", $("#MenusModify")).val(idsUp);
        debugger;
        var oldmenudishesNo = $("#hidFoodIdsMyMenu", $("#MenusModify")).val();
        var i = oldmenudishesNo.indexOf(trobj.eq(0).text() + ",");
        var m = oldmenudishesNo.substring(0, i);
        var n = oldmenudishesNo.substring(i + 7);
        $("#hidFoodIdsMyMenu", $("#MenusModify")).val(m + n);
       
    }

    function Getdelete(obj) {
        debugger;
        var trobj = $(obj).parent().parent().parent().find("td");
        var idsDel = $("#hidDel", $("#MenusModify")).val();
        idsDel += trobj.eq(0).text() + ",";
        $("#hidDel", $("#MenusModify")).val(idsDel);
        var oldmenudishesNo = $("#hidFoodIdsMyMenu", $("#MenusModify")).val();
        var i = oldmenudishesNo.indexOf(trobj.eq(0).text() + ",");
        var m = oldmenudishesNo.substring(0, i);
        var n = oldmenudishesNo.substring(i + 7);
        $("#hidFoodIdsMyMenu", $("#MenusModify")).val(m + n);
        trobj.hide();

    }


    //每次批量带回前确定明细里已有的，推送批量带回框，进行筛选，已有不显示checkbox，实现唯一性
    function addChosedIdMyMenu() {
        var foodIds = "";
        $("table[addid='addMyMenuDetail']", $("#MyMenuModify")).find("input[hidty='foodId']").each(function (i, n) {
            foodIds = foodIds + n.value + ",";
        });
        $("#hidFoodIdsMyMenu", $("#MyMenuModify")).val(foodIds);

    }

    function SelectedDishesList() {
        debugger;
        //推送到拼接控件的界面，表示本次带回主键id
        var ids = $("#selectedDishes_No", $("#MenusModify")).val();
        $.post("MenusManage/AddBatchFood.aspx", { ids: ids }, function (da) {
            var das = da.split('|');
            if (das.length == 2) {
                $(das[0]).appendTo($("tbody", $("#MenusModify")));
                $("tbody", $("#MenusModify")).initUI().find("a.btnDel").click(function () {
                    //获取要移除的主键id
                    var delrId = $(this).attr("rId");
                    alert(delrId);
                    //获取之前所有新增明细的id拼接字符串
                    var nowAddPrimaryKeyIdsStr = $("#addMyMenuSelectedIds", $("#MenusModify")).val();

                    //获取要删除的主键id的索引
                    var delIndex = parent.GetDeleteKeyIdIndex(nowAddPrimaryKeyIdsStr, delrId);

                    //移除要删除的id
                    var deledStr = parent.GetDeletedStr(nowAddPrimaryKeyIdsStr, delIndex);

                    //赋值移除后的id拼接字符串
                    $("#addMyMenuSelectedIds", $("#MenusModify")).val(deledStr);
                    $(this).parents("tr:first").remove();

                });
            }
            //获取之前新增的id拼接字符串
            var addMyMenuSelectedIds = $("#addMyMenuSelectedIds", $("#MenusModify")).val();
            //之前新增的id拼接字符串，加上本次带回来的新增id拼接字符串
            $("#addMyMenuSelectedIds", $("#MenusModify")).val(addMyMenuSelectedIds + das[1]);

        });
    }
    /*移除新增明细删除项的索引*/
    function GetDeleteKeyIdIndex(primaryKeyIds, deletePrimaryKeyId) {
        var index = -1;
        if (primaryKeyIds == undefined || primaryKeyIds == '') {
            index = -1;
        } else if (primaryKeyIds.indexOf(',') < 0) {
            if (primaryKeyIds == deletePrimaryKeyId.toString()) {
                index = 0;

            } else {
                index = -1;
            }
        } else {
            var primaryKeyIdArr = primaryKeyIds.toString().split(',');

            for (var i = 0; i < primaryKeyIdArr.length; i++) {
                if (primaryKeyIdArr[i] == deletePrimaryKeyId) {
                    index = i;
                    break;
                }
            }
        }
        return index;
    }

    function GetDeletedStr(oldStr, deleteIndex) {
        var resultStr = '';
        if (oldStr == undefined || oldStr == '') {
            return resultStr;
        }
        var oldStrArr = oldStr.toString().split(',');
        if (deleteIndex < 0 || deleteIndex > oldStrArr.length - 1) {
            resultStr = oldStr;
            return resultStr;
        } else {
            oldStrArr.splice(deleteIndex, 1);

            for (var i = 0; i < oldStrArr.length; i++) {
                if (oldStrArr[i].toString() != '') {
                    resultStr += oldStrArr[i].toString() + ',';
                }
            }
        }

        return resultStr;
    }

</script>
<div class="pageContent" height="700px">
    <form method="post" id="MenusModify" action="MenusManage/MenusH.aspx?type=modify"
    class="pageForm required-validate" onsubmit="return validateCallback(this, dialogAjaxDone);">
    <div class="pageFormContent ">
        <input name="hid" type="hidden" value="<%=hid %>" />
        <div class="pageFormContent">
            <p>
                <label>
                    菜单编号：</label>
                <input name="Menus_No" type="text" size="20" value="<%=Menus_No%>" readonly="readonly" />
            </p>
            <p>
                <label>
                    总金额：</label>
                <input name="Money" type="text" size="20" readonly="readonly" value="<%=Money%>"
                    maxlength='20' onkeyup="inputNum(this,20,'errorname1')" />
                <span id="errorname1" generated="true" class="error" style="margin-left: 25px; display: none">
                    不得超过20个字符</span>
            </p>
            <p>
                <label>
                    总数量：</label>
                <input type="text" name="Num" value=" <%=Num %>" readonly="readonly" />
            </p>
            <p>
                <label>
                    员工：</label>
                <input name="User_No" id="User_No" type="text" size="20" value="<%=User_No %>" />
            </p>
            <p>
                <label>
                    备注：</label>
                <input type="text" name="Remark" style="width: 240px;" value=" <%=Remark %>" />
            </p>
        </div>
    </div>
    <div class="panelBar">
        <ul class="toolBar">
            <li><a type="button" lookupgroup="empLookup" target="dialog" width="800" height="600"
                href="MenusManage/selectDishesLookUp.aspx" callback="SelectedDishesList();">添加</a>
                <input type="hidden" name="empLookup.DishesNo" id="selectedDishes_No" style="float: right;" />
            </li>
        </ul>
    </div>
    <input type="hidden" id="hidFoodIdsMyMenu" name="hidFoodIdsMyMenu" value="<%=AddMyMenuSelectedIds %>" />
    <%-- 存放已经添加的菜品编号--%>
    <table id="menudisheslist" class="table" width="100%" layouth="220" unique="hidFoodIdsMyMenu">
        <thead>
            <tr>
                <th  >
                    菜品编号
                </th>
                <th >
                    菜品名称
                </th>
                <th >
                    单价
                </th>
                <th >
                    数量
                </th>
                <th> 
                    操作
                </th>
            </tr>
            <%-- 存放要删除的菜品编号 （可能包含部分要修改的菜品编号，此时的操作为对菜品先修改再删除，这种情况只执行删除操作！）--%>
            <input type="hidden" id="hidDel" name="hidDel" value="" />
            <%--存放要修改的菜品编号--%>
            <input type="hidden" id="hidUpd" name="hidUpd" value="" />
        </thead>
        <tbody>
            <%--存放新添加的菜品编号--%>
            <input type="hidden" id="addMyMenuSelectedIds" name="addMyMenuSelectedIds" value="" />
            <asp:literal id="MenusLitreal" runat="server"></asp:literal>
        </tbody>
    </table>
    <div class="formBar">
        <ul>
            <li>
                <div class="buttonActive">
                    <div class="buttonContent">
                        <button type="submit">
                            保存</button></div>
                </div>
            </li>
            <li>
                <div class="button">
                    <div class="buttonContent">
                        <button type="button" class="close">
                            取消</button>
                    </div>
                </div>
            </li>
        </ul>
    </div>
    </form>
</div>
